/**
 * inforcreation.com co.Ltd
 * MichaelHuang
 * 2011-12-16
 */
package com.zchat.api.connection;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.CacheManager;

import com.zchat.android.receivers.ReadStatusReceiver;
import com.zchat.api.cache.CacheManagerImple;
import com.zchat.api.log.Logger;
import com.zchat.api.util.ZChatSetting;

/**
 * Recognize current read status.
 * @author Oliverzhu
 *
 */
public class ReadStatusUtil {
	private static final String TAG = "ReadStatusUtil";
	public static final boolean DEBUG = ZChatSetting.DEBUG;

	private ReadStatusUtil () {
		
	}
	private static ReadStatusUtil instance = new ReadStatusUtil();
	public static ReadStatusUtil getInstance() {
		return instance;
	}
	
	private ReadStatus status = ReadStatus.online;	//default is online read status.
	
	/**
	 * Set the read status
	 * @param status
	 */
	public synchronized void setStatus(Context context, ReadStatus status) {
		this.status = status;
		//get pre status from cache
		CacheManagerImple cacheManager = CacheManagerImple.getInstance();
		if(cacheManager != null) {
			Object value =  cacheManager.get("read_status");
			if(value != null) {
				ReadStatus preStatus = ReadStatus.stringToEnum(value.toString());
				if(preStatus == status) {
					return;
				}
			}
			
		}
		if(DEBUG) Logger.info(TAG+"-Read status has changed to : " + status.toString());
		//send braodcaster
		Intent bc = new Intent(ReadStatusReceiver.READ_STATUS_CHANGED);
		context.sendBroadcast(bc);
	}
	
	/**
	 * Get current read status.
	 * @return
	 */
	public ReadStatus getStatus() {
		return status;
	}
}
